package com.scsj.cloud.quartz.web;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.scsj.cloud.quartz.data.model.JobAndTrigger;
import com.scsj.cloud.quartz.data.model.JobInfo;
import com.scsj.cloud.quartz.service.IJobAndTriggerService;
import com.scsj.cloud.quartz.service.JobService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/scsj/cloud/job")
@Api(value = "任务(job)相关")
public class JobController {

    @Autowired
    private JobService jobService;
    
    @Autowired
    private IJobAndTriggerService iJobAndTriggerService;

    private static Logger logger = LoggerFactory.getLogger(JobController.class);
    
    
    /**
     * 添加任务
     *
     * @param jobInfo
     * @throws Exception
     */
    @PostMapping(value = "/addJob")
    @ApiOperation(value = "新增任务", notes = "参数：任务对象")
    public void addjob(@RequestBody JobInfo jobInfo) throws Exception {
    	jobService.addjob(jobInfo);
    }

    /**
     * 暂停任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @throws Exception
     */
    @PostMapping(value = "/pauseJob")
    @ApiOperation(value = "暂停任务", notes = "参数：任务名，组名")
    public void pausejob(@RequestParam(value = "jobClassName") String jobClassName, 
    		@RequestParam(value = "jobGroupName") String jobGroupName) 
    		throws Exception {
    	jobService.pausejob(jobClassName, jobGroupName);
    }

    /**
     * 恢复任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @throws Exception
     */
    @PostMapping(value = "/resumeJob")
    @ApiOperation(value = "恢复任务", notes = "参数：任务名，组名")
    public void resumejob(@RequestParam(value = "jobClassName") String jobClassName, @RequestParam(value = "jobGroupName") String jobGroupName) 
    		throws Exception {
    	jobService.resumejob(jobClassName, jobGroupName);
    }

    /**
     * 更新任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @param cronExpression
     * @throws Exception
     */
    @PostMapping(value = "/rescheduleJob")
    @ApiOperation(value = "更新任务", notes = "参数：任务名，组名，cron表达式")
    public void rescheduleJob(@RequestParam(value = "jobClassName") String jobClassName,
                              @RequestParam(value = "jobGroupName") String jobGroupName,
                              @RequestParam(value = "cronExpression") String cronExpression) throws Exception {
    	jobService.rescheduleJob(jobClassName, jobGroupName, cronExpression);
    }

    /**
     * 删除任务
     * 删除操作前应该暂停该任务的触发器，并且停止该任务的执行
     *
     * @param jobClassName
     * @param jobGroupName
     * @throws Exception
     */
    @PostMapping(value = "/deletejob")
    @ApiOperation(value = "删除任务", notes = "参数：任务名，组名")
    public void deletejob(@RequestParam(value = "jobClassName") String jobClassName, @RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
        jobService.deletejob(jobClassName, jobGroupName);
    }

    /**
     * 查询任务
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/queryJob")
    @ApiOperation(value = "查询任务", notes = "参数：分页条件")
    public Map<String, Object> queryjob(@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
        PageInfo<JobAndTrigger> jobAndTrigger = iJobAndTriggerService.getJobAndTriggerDetails(pageNum, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("JobAndTrigger", jobAndTrigger);
        map.put("number", jobAndTrigger.getTotal());
        return map;
    }

    

}
